import libtcodpy as libtcod

import splash
import utils

class Town(object):
    def __init__(self, log):
        smap = ['7777777#########################################################################',
                '777777##########################################################################',
                '7777                                                                          ##',
                '777                             555555555555     333333333333                 ##',
                '7777                            555555555555     333333333333                 ##',
                '77777                           555555555555  999433333333333                 ##',
                '7777777                         555555555     9  333333333333                 ##',
                '7777777777                      555555556999999  333333333333                 ##',
                '7777777777777777                555555555    *9                               ##',
                '7777777777777777777                           9                               ##',
                '77777777777777777777                          9                               ##',
                '777777777777777777777                         9                               ##',
                '777777777777777777777      99999999999999     9                               ##',
                '77777777777777777777*      99999999999999    *9        *          *          *##',
                '77777777777777777779999999999* 000000 *999999999999999999999999999999999999999##',
                '77777777777777777789999999999 00000000 999999999999999999999999999999999999999ab',
                '77777777777777777779999999999* 000000 *999999999999999999999999999999999999999##',
                '77777777777777777777*      99999999999999     9        *          *          *##',
                '777777777777777777777      99999999999999     9                               ##',
                '777777777777777777777            99           9                               ##',
                '777777777777777777777      *   * 99 *   *     9           1111111111111       ##',
                '7777777777777777777777     ccccc 99 ccccc     9*         *1111111111111       ##',
                '7777777777777777777777     ccccccddcccccc     9999999999992111111111111       ##',
                '7777777777777777777777     cccccccccccccc                 1111111111111       ##',
                '777777777777777777777      cccccccccccccc                 1111111111111       ##',
                '77777777777777777777       cccccccccccccc                                     ##',
                '77777777777777777777       cccccccccccccc                                     ##',
                '7777777777777777777                                                           ##',
                '77777777777777777###############################################################',
                '7777777777777777################################################################',
               ]
        tiles = {'#': utils.Tile(libtcod.light_grey, libtcod.grey, False, False),
                 '1': utils.Tile(libtcod.dark_magenta, libtcod.dark_magenta, False, False),
                 '2': utils.Tile(libtcod.desaturated_magenta, libtcod.desaturated_magenta, True, True,
                                 libtcod.CHAR_DCROSS, libtcod.magenta, 'inne'),
                 '3': utils.Tile(libtcod.dark_red, libtcod.dark_red, False, False),
                 '4': utils.Tile(libtcod.desaturated_red, libtcod.desaturated_red, True, True, 
                                 libtcod.CHAR_DCROSS, libtcod.red, 'armor'),
                 '5': utils.Tile(libtcod.dark_red, libtcod.dark_red, False, False),
                 '6': utils.Tile(libtcod.desaturated_red, libtcod.desaturated_red, True, True, 
                                 libtcod.CHAR_DCROSS, libtcod.red, 'weapons'),
                 '7': utils.Tile(libtcod.dark_grey, libtcod.darker_grey, False, False, name='mountain'),
                 '8': utils.Tile(libtcod.grey, libtcod.grey, True, True, libtcod.CHAR_DCROSS, libtcod.white, 'hall'),
                 '9': utils.Tile(libtcod.dark_orange, libtcod.dark_orange, True, True, 
                                 libtcod.CHAR_BLOCK1, libtcod.desaturated_orange),
                 '0': utils.Water(),
                 'a': utils.Tile(libtcod.dark_orange, libtcod.dark_orange, True, True, 
                                 libtcod.CHAR_BLOCK1, libtcod.desaturated_orange, name='entry'),
                 'b': utils.Tile(libtcod.dark_orange, libtcod.dark_orange, True, True, 
                                 libtcod.CHAR_BLOCK1, libtcod.desaturated_orange, name='exit'),
                 'c': utils.Tile(libtcod.dark_yellow, libtcod.dark_yellow, False, False),
                 'd': utils.Tile(libtcod.desaturated_yellow, libtcod.desaturated_yellow, True, True, 
                                 libtcod.CHAR_DCROSS, libtcod.yellow, 'retire'),
                 ' ': utils.Tile(libtcod.dark_green, libtcod.darker_green, True, True, speed=100, name='grass'),
                 '*': utils.Light(libtcod.light_yellow, 10, char_color=libtcod.dark_orange),
                }

        log('Landscaping terrain.')
        grass_noise = utils.get_noise(len(smap[0]), len(smap), libtcod.Color(32, 128, 32), 3.0)
        mountain_noise = utils.get_noise(len(smap[0]), len(smap), libtcod.Color(64, 64, 64), 8.0, libtcod.noise_wavelet)

        self.lights = []
        self.map = []
        check_width = len(smap[0]) # When you see error checking in a 7DRL you know there's been frustration.

        log('Building town.')
        for y, row in enumerate(smap):
            assert len(row) == check_width

            row_tiles = []
            for x, col in enumerate(row):
                tile = tiles[col]
                if isinstance(tile, utils.Light):
                    tile.x = x
                    tile.y = y
                    self.lights.append(utils.Light(libtcod.light_yellow, 6, x, y, char_color=libtcod.dark_orange))
                elif tile.name == 'grass':
                    bright = grass_noise.get_pixel(x, y)
                    bright.g += 64
                    dark = bright * 0.5
                    tile = utils.Tile(bright, dark, True, True, speed=100, name='grass')
                elif tile.name == 'mountain':
                    dark = mountain_noise.get_pixel(x, y) + libtcod.Color(32, 16, 16)
                    bright = dark * 1.5
                    tile = utils.Tile(bright, dark, False, True, name='mountain')
                row_tiles.append(tile)
            self.map.append(row_tiles)

        log('Posting notices.')
        notes = [(26, 0, dict(text='== Welcome to Roguestock ==', key_hit=False, background_alpha=0.0)),
                 (30, 24, dict(text='Hall  Of\n  Fame', key_hit=False, foreground=libtcod.yellow, 
                               background_alpha=0.0)),
                 (52, 4, dict(text='Armour\n\nShoppe', key_hit=False, foreground=libtcod.red, background_alpha=0.0)),
                 (33, 4, dict(text='Weapon\n\nShoppe', key_hit=False, foreground=libtcod.red, background_alpha=0.0)),
                 (60, 21, dict(text='Rest For\n   The\n Wicked', key_hit=False, foreground=libtcod.light_magenta, 
                              background_alpha=0.0)),
                 (5, 14, dict(text='Iron Prison\n\nBar & Grill', key_hit=False, foreground=libtcod.light_gray, 
                              background_alpha=0.0)),
                 (0, 28, dict(text="Press F1\nfor help.", key_hit=False, foreground=libtcod.grey,
                              background_alpha=0.0)),
                ]
        self.notices = [dict(pos=(n[0], n[1]), note=splash.Splash(**n[2])) for n in notes]
        log('Town complete.')

    @property
    def height(self):
        return len(self.map)
    @property
    def width(self):
        return len(self.map[0])



if __name__ == '__main__':
    import console

    libtcod.console_set_custom_font('fonts/dejavu12x12_gs_tc.png', 
                                    libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    town = Town()
    root = console.RootConsole(town.width, town.height, 'The Town')

    for y, row in enumerate(town.map):
        for x, tile in enumerate(row):
            char = ' ' if tile.char is None else tile.char
            root.putchar_ex(x, y, char, tile.color, tile.light)
    root.flush()

    print town.width, town.height

    print "Press any key to exit..."
    root.wait_for_keypress(True)









